// WVP登录认证接口
export async function POST(request) {
  try {
    const { username, password } = await request.json();
    const wvpBaseUrl = process.env.WVP_BASE_URL || 'http://222.222.25.198:18082';
    
    console.log('WVP登录请求:', { username, wvpBaseUrl });
    
    // 调用WVP登录接口
    const loginUrl = `${wvpBaseUrl}/api/user/login`;
    const response = await fetch(loginUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        username,
        password
      })
    });

    console.log('WVP登录响应状态:', response.status);

    if (!response.ok) {
      const errorText = await response.text();
      console.error('WVP登录失败:', errorText);
      throw new Error(`登录失败: ${response.status} ${response.statusText}`);
    }

    const data = await response.json();
    console.log('WVP登录成功:', { code: data.code, msg: data.msg });

    // 检查登录是否成功
    if (data.code !== 0) {
      throw new Error(data.msg || '登录失败');
    }

    return Response.json({
      success: true,
      token: data.data?.token || data.token,
      userInfo: data.data,
      message: '登录成功'
    });

  } catch (error) {
    console.error('登录接口错误:', error);
    
    return Response.json({
      success: false,
      error: error.message,
      timestamp: new Date().toISOString()
    }, { status: 500 });
  }
}